﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Timers;


namespace GlucometerPrototype
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Glucometer : Window
    {
        Device device = new Device();
        //here i had tried to use relative path many times but all failed
        BitmapImage imgBatteryLevel0 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\batteryLevel0.png", UriKind.Absolute));
        BitmapImage imgBatteryLevel1 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\batteryLevel1.png", UriKind.Absolute));
        BitmapImage imgBatteryLevel2 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\batteryLevel2.png", UriKind.Absolute));
        BitmapImage imgBatteryLevel3 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\batteryLevel3.png", UriKind.Absolute));
        BitmapImage imgBatteryLevel4 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\batteryLevel4.png", UriKind.Absolute));

        //for the display of test result, 0~9
        BitmapImage imgNumber0 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\0.png", UriKind.Absolute));
        BitmapImage imgNumber1 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\1.png", UriKind.Absolute));
        BitmapImage imgNumber2 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\2.png", UriKind.Absolute));
        BitmapImage imgNumber3 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\3.png", UriKind.Absolute));
        BitmapImage imgNumber4 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\4.png", UriKind.Absolute));
        BitmapImage imgNumber5 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\5.png", UriKind.Absolute));
        BitmapImage imgNumber6 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\6.png", UriKind.Absolute));
        BitmapImage imgNumber7 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\7.png", UriKind.Absolute));
        BitmapImage imgNumber8 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\8.png", UriKind.Absolute));
        BitmapImage imgNumber9 = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\9.png", UriKind.Absolute));
        
        //
        BitmapImage imgUnitL = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\unitL.png", UriKind.Absolute));
        BitmapImage imgUnitDL = new BitmapImage(new Uri(System.Environment.CurrentDirectory + @"\Images\unitDL.png", UriKind.Absolute));

        //
        Timer timerFlash = new Timer(1000);
        Timer timerProgressiveBar = new Timer(500);
        Timer timerClick = new Timer(2000);
        bool bDoubleClick = false;
        

        public Glucometer()
        {
            InitializeComponent();
            //
            refreshButtonVisibility();
            timerFlash.Elapsed += new ElapsedEventHandler(timerFlash_Elapsed);
            timerProgressiveBar.Elapsed += new ElapsedEventHandler(timerProgressiveBar_Elapsed);
            timerClick.Elapsed += new ElapsedEventHandler(timerClick_Elapsed);
        }

        void timerClick_Elapsed(object sender, ElapsedEventArgs e)
        {
            bDoubleClick = true;
            MessageBox.Show("Hello");
            timerClick.Stop();
            throw new NotImplementedException();
        }

        private void refreshButtonVisibility()
        {
            if (device.USBStatus)
            {
                button_USB_plug.Visibility = Visibility.Collapsed;
                button_USB_pluck.Visibility = Visibility.Visible;
            }
            else
            {
                button_USB_plug.Visibility = Visibility.Visible;
                button_USB_pluck.Visibility = Visibility.Collapsed;
            }
            if (device.ACpluginStatus)
            {
                button_AC_plug.Visibility = Visibility.Collapsed;
                button_AC_pluck.Visibility = Visibility.Visible;
            }
            else
            {
                button_AC_plug.Visibility = Visibility.Visible;
                button_AC_pluck.Visibility = Visibility.Collapsed;
            }
            if (device.TestStripStatus)
            {
                button_TestStrip_insert.Visibility = Visibility.Collapsed;
                button_TestStrip_pluck.Visibility = Visibility.Visible;
                if (device.Feed)
                {
                    button_TestStrip_Feed.Visibility = Visibility.Hidden;
                }
                else
                {
                    button_TestStrip_Feed.Visibility = Visibility.Visible;
                }
            }
            else
            {
                button_TestStrip_insert.Visibility = Visibility.Visible;
                button_TestStrip_pluck.Visibility = Visibility.Collapsed;
                button_TestStrip_Feed.Visibility = Visibility.Collapsed;
            }
            if (device.On)
            {
                button_Error_Make.Visibility = Visibility.Visible;
                button_progressiveBar_move.Visibility = Visibility.Visible;
            }
            else
            {
                button_Error_Make.Visibility = Visibility.Collapsed;
                button_progressiveBar_move.Visibility = Visibility.Hidden;
            }
        }

        private void button_main_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //mouse down
            //start the timer
            timerClick.Start();
        }

        private void command_Exit(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void turn_on()
        {
            Initialization();
            refreshButtonVisibility();
            //show the progressive bar
            showProgressiveBar(3);
        }

        //the timeSpan is in seconds
        private void showProgressiveBar(double timeSpan)
        {
            //DateTime endTime = DateTime.Now.AddSeconds(timeSpan);

            image_progressiveBar.Visibility = Visibility.Visible;
            timerProgressiveBar.Start();
            //while (DateTime.Now != endTime)
            //{
            //    //move the image_progressiveBar
            //    if (DateTime.Now.Millisecond < 500)
            //    {
            //        //

            //    }
            //}
            //newMargin.Left = 0;
            //image_progressiveBar.Margin = newMargin;
            //image_progressiveBar.Visibility = Visibility.Hidden;
        }

        private void moveProgressiveBar()
        {
            if (device.Error)
            {
                return;
            }
            Thickness newMargin = new Thickness();
            newMargin = image_progressiveBar.Margin;
            if (newMargin.Left <= 140)
            {
                newMargin.Left += 5;
            }
            else
            {
                newMargin.Left = 0;
            }
            image_progressiveBar.Margin = newMargin;
        }

        private void Initialization()
        {
            setDateTime(device.Time);
            showDateTime();
            showDeviceStatus();
            showIdleResult();
            device.On = true;
            device.Error = false;
        }

        private void showIdleResult()
        {
            image_result1.Source = imgNumber0;
            image_result2.Source = imgNumber0;
            image_result3.Source = imgNumber0;
            image_result1.Visibility = Visibility.Visible;
            image_result2.Visibility = Visibility.Visible;
            image_result3.Visibility = Visibility.Visible;
            //unit
            image_resultUnit.Visibility = Visibility.Visible;
            if (device.Unit.Equals("L"))
            {
                //
                image_resultDot.Visibility = Visibility.Visible;
            }
            else if (device.Unit.Equals("DL"))
            {
                //
                image_resultDot.Visibility = Visibility.Hidden;
            }
        }

        private void shutdown()
        {
            //hide Date
            textBlock_date_month.Visibility = Visibility.Hidden;
            textBlock_date_separator1.Visibility = Visibility.Hidden;
            textBlock_date_day.Visibility = Visibility.Hidden;
            textBlock_date_separator2.Visibility = Visibility.Hidden;
            textBlock_date_year.Visibility = Visibility.Hidden;
            //hide Time
            textBlock_time_hour.Visibility = Visibility.Hidden;
            textBlock_time_separator1.Visibility = Visibility.Hidden;
            textBlock_time_minute.Visibility = Visibility.Hidden;
            textBlock_date_day.Visibility = Visibility.Hidden;
            textBlock_date_day.Visibility = Visibility.Hidden;
            //hide progressive bar
            image_progressiveBar.Visibility = Visibility.Hidden;
            //hide result
            image_result1.Visibility = Visibility.Hidden;
            image_result2.Visibility = Visibility.Hidden;
            image_resultDot.Visibility = Visibility.Hidden;
            image_result3.Visibility = Visibility.Hidden;
            image_resultUnit.Visibility = Visibility.Hidden;
            //hide device status
            image_batteryLevel.Visibility = Visibility.Hidden;
            image_ACStatus.Visibility = Visibility.Hidden;
            image_USBStatus.Visibility = Visibility.Hidden;
            image_errorStatus.Visibility = Visibility.Hidden;
        }

        private void setDateTime(DateTime time)
        {
            //Date
            string month = time.Month.ToString();
            string day = time.Day.ToString();
            string year = (time.Year % 100).ToString();
            if (month.Length == 1)
            {
                month = "0" + month;
            }
            if (day.Length == 1)
            {
                day = "0" + day;
            }
            if (year.Length == 1)
            {
                year = "0" + day;
            }
            textBlock_date_month.Text = month;
            textBlock_date_day.Text = day;
            textBlock_date_year.Text = year;
            //Time
            string hour = time.Hour.ToString();
            string minute = time.Minute.ToString();
            if (hour.Length == 1)
            {
                hour = "0" + hour;
            }
            if (minute.Length == 1)
            {
                minute = "0" + minute;
            }
            textBlock_time_hour.Text = hour;
            textBlock_time_minute.Text = minute;
        }

        private void showDateTime()
        {
            //show Date
            textBlock_date_month.Visibility = Visibility.Visible;
            textBlock_date_separator1.Visibility = Visibility.Visible;
            textBlock_date_day.Visibility = Visibility.Visible;
            textBlock_date_separator2.Visibility = Visibility.Visible;
            textBlock_date_year.Visibility = Visibility.Visible;
            //Show Time
            textBlock_time_hour.Visibility = Visibility.Visible;
            textBlock_time_separator1.Visibility = Visibility.Visible;
            textBlock_time_minute.Visibility = Visibility.Visible;
            textBlock_date_day.Visibility = Visibility.Visible;
            textBlock_date_day.Visibility = Visibility.Visible;
        }

        private void showDeviceStatus()
        {
            refreshBatteryLevel();
            image_batteryLevel.Visibility = Visibility.Visible;
            image_ACStatus.Visibility = (device.ACpluginStatus == true) ? Visibility.Visible : Visibility.Hidden;
            image_USBStatus.Visibility = (device.USBStatus == true) ? Visibility.Visible : Visibility.Hidden;
            image_errorStatus.Visibility = (device.Error == true) ? Visibility.Visible : Visibility.Hidden;
        }

        private void refreshBatteryLevel()
        {
            switch (device.BatteryLevel)
            {
                case 0:
                    image_batteryLevel.Source = imgBatteryLevel0;
                    break;
                case 1:
                    image_batteryLevel.Source = imgBatteryLevel1;
                    break;
                case 2:
                    image_batteryLevel.Source = imgBatteryLevel2;
                    break;
                case 3:
                    image_batteryLevel.Source = imgBatteryLevel3;
                    break;
                case 4:
                    image_batteryLevel.Source = imgBatteryLevel4;
                    break;
                default:
                    break;
            }
        }

        private void command_action_USB_Connected(object sender, RoutedEventArgs e)
        {
            action_USB_Connected();
        }

        private void action_USB_Connected()
        {
            device.USBStatus = true;
            textBlock_USB_Status.Text = "Connected";
            if (device.Error)
            {
                return;
            }
            if (!device.On)
            {
                //turn on the device
                turn_on();
            }
            else
            {
                image_USBStatus.Visibility = Visibility.Visible;
            }
            refreshButtonVisibility();
        }

        private void command_action_ACplug_Connected(object sender, RoutedEventArgs e)
        {
            action_AC_Connected();
        }

        private void action_AC_Connected()
        {
            device.ACpluginStatus = true;
            textBlock_AC_Status.Text = "Connected";
            refreshButtonVisibility();
            if (device.Error)
            {
                return;
            }
            if (!device.On)
            {
                //turn on the device
                turn_on();
            }
            else
            {
                image_ACStatus.Visibility = Visibility.Visible;
            }
        }

        private void command_action_TestStrip_Inserted(object sender, RoutedEventArgs e)
        {
            action_TestStrip_Inserted();
        }

        private void action_TestStrip_Inserted()
        {
            device.TestStripStatus = true;
            textBlock_TestStrip_Status.Text = "Inserted";
            refreshButtonVisibility();
            if (device.Error)
            {
                return;
            }
            image_testStrip.Visibility = Visibility.Visible;
            if (!device.On)
            {
                //turn on the device
                turn_on();
            }
            if (device.Feed)
            {
                action_feed_bloodSample();
            }
        }

        private void command_action_ACplug_NOT_Connected(object sender, RoutedEventArgs e)
        {
            action_ACplug_NOT_Connected();
        }

        private void action_ACplug_NOT_Connected()
        {
            device.ACpluginStatus = false;
            textBlock_AC_Status.Text = "Not Connected";
            refreshButtonVisibility();
            if (device.Error)
            {
                return;
            }
            image_ACStatus.Visibility = Visibility.Hidden;
        }

        private void command_action_USB_NOT_Connected(object sender, RoutedEventArgs e)
        {
            action_USB_NOT_Connected();
        }

        private void action_USB_NOT_Connected()
        {
            device.USBStatus = false;
            textBlock_USB_Status.Text = "Not Connected";
            refreshButtonVisibility();
            if (device.Error)
            {
                return;
            }
            image_USBStatus.Visibility = Visibility.Hidden;
        }

        private void command_action_TestStrip_NOT_Inserted(object sender, RoutedEventArgs e)
        {
            action_TestStrip_NOT_Inserted();
        }

        private void action_TestStrip_NOT_Inserted()
        {
            device.TestStripStatus = false;
            device.Feed = false;
            textBlock_TestStrip_Status.Text = "Not Inserted";
            refreshButtonVisibility();
            if (device.Error)
            {
                return;
            }
            image_testStrip.Visibility = Visibility.Hidden;
        }

        private void button_USB_plug_Click(object sender, RoutedEventArgs e)
        {
            action_USB_Connected();
        }

        private void button_USB_pluck_Click(object sender, RoutedEventArgs e)
        {
            action_USB_NOT_Connected();
        }

        private void button_AC_plug_Click(object sender, RoutedEventArgs e)
        {
            action_AC_Connected();
        }

        private void button_AC_pluck_Click(object sender, RoutedEventArgs e)
        {
            action_ACplug_NOT_Connected();
        }

        private void button_TestStrip_insert_Click(object sender, RoutedEventArgs e)
        {
            action_TestStrip_Inserted();
        }

        private void button_TestStrip_pluck_Click(object sender, RoutedEventArgs e)
        {
            action_TestStrip_NOT_Inserted();
        }

        private void button_main_Click(object sender, RoutedEventArgs e)
        {
            return;
            // release
            //if (bDoubleClick)
            //{
            //    action_button_doublePress();
            //    bDoubleClick = false;
            //}
            //else
            //{
            //    action_button_shortPress();
            //}
        }

        private void action_feed_bloodSample()
        {
            device.Feed = true;
            textBlock_TestStrip_Status.Text = "Feed with Blood Sample";
            refreshButtonVisibility();
            if (device.Error)
            {
                return;
            }
            //
            showTestResult();
        }

        private void showTestResult()
        {
            if (device.Unit.Equals("L"))
            {
                //result is 00.0~99.9
                image_result1.Source = imgNumber5;
                image_result2.Source = imgNumber5;
                image_result3.Source = imgNumber8;
                image_result1.Visibility = Visibility.Visible;
                image_result2.Visibility = Visibility.Visible;
                image_result3.Visibility = Visibility.Visible;
            }
            else if (device.Unit.Equals("DL"))
            {
                //result is 0~999
                image_result1.Source = imgNumber1;
                image_result2.Source = imgNumber6;
                image_result3.Source = imgNumber3;
                image_result1.Visibility = Visibility.Visible;
                image_result2.Visibility = Visibility.Visible;
                image_result3.Visibility = Visibility.Visible;
            }
            showTestResultDot();
            image_resultUnit.Visibility = Visibility.Visible;
        }

        private void showTestResultDot()
        {
            if (device.Unit.Equals("L"))
            {
                //result is 00.0~99.9
                image_resultDot.Visibility = Visibility.Visible;
            }
            else if (device.Unit.Equals("DL"))
            {
                //result is 0~999
                image_resultDot.Visibility = Visibility.Hidden;
            }
        }

        private void button_TestStrip_Feed_Click(object sender, RoutedEventArgs e)
        {
            action_feed_bloodSample();
        }

        private void button_Error_Make_Click(object sender, RoutedEventArgs e)
        {
            action_Error_Make();
        }

        private void action_Error_Make()
        {
            device.Error = true;
            textBlock_error.Text = "Erroring";
            image_errorStatus.Visibility = Visibility.Visible;
            //show error code
            image_result1.Source = imgNumber0;
            image_result2.Source = imgNumber0;
            image_result3.Source = imgNumber2;
            image_resultDot.Visibility = Visibility.Hidden;
            image_resultUnit.Visibility = Visibility.Hidden;
        }

        private void button_button_short_Click(object sender, RoutedEventArgs e)
        {
            action_button_shortPress();
        }

        private void action_button_shortPress()
        {
            textBlock_Button_Status.Text = "Short press";
            if (!device.On)
            {
                return;
            }
            if (device.Error)
            {
                return;
            }
            //
            if (device.SetArea == Device.Setting.nothing)
            {
                //it is in the browse mode, broswe the previous-time result
            }
            else
            {
                //change the value of setting area
                DateTime tempTime = device.Time;
                switch (device.SetArea)
                {
                    case Device.Setting.unit:
                        if (device.Unit.Equals("L"))
                        {
                            device.Unit = "DL";
                            image_resultUnit.Source = imgUnitDL;
                        }
                        else
                        {
                            device.Unit = "L";
                            image_resultUnit.Source = imgUnitL;
                        }
                        showTestResultDot();
                        break;
                    case Device.Setting.month:
                        if (device.Time.Month == 12)
                        {
                            device.Time = device.Time.AddMonths(-11);
                        }
                        else
                        {
                            device.Time = device.Time.AddMonths(1);
                        }
                        setDateTime(device.Time);
                        showDateTime();
                        break;
                    case Device.Setting.day:
                        device.Time = device.Time.AddDays(1);
                        setDateTime(device.Time);
                        break;
                    case Device.Setting.year:
                        if (device.Time.Year == 2015)
                        {
                            device.Time = device.Time.AddYears(-5);
                        }
                        else
                        {
                            device.Time = device.Time.AddYears(1);
                        }
                        setDateTime(device.Time);
                        break;
                    case Device.Setting.hour:
                        device.Time = device.Time.AddHours(1);
                        setDateTime(device.Time);
                        break;
                    case Device.Setting.minute:
                        device.Time = device.Time.AddMinutes(1);
                        setDateTime(device.Time);
                        break;
                    default:
                        break;
                }
            }
        }

        private void action_button_longPress()
        {
            textBlock_Button_Status.Text = "Long press";
            if (!device.On)
            {
                turn_on();
            }
            if (device.Error)
            {
                return;
            }
            if (device.BrowseHistory)
            {
                //quit the state of browsing
                device.BrowseHistory = false;
                setDateTime(device.Time);
            }
            if (device.SetArea != Device.Setting.nothing)
            {
                //quit the setting mode
                device.SetArea = Device.Setting.nothing;
                textBlock_Setting_Status.Text = "Null";
                image_resultUnit.Visibility = Visibility.Hidden;
            }
        }

        private void action_button_doublePress()
        {
            textBlock_Button_Status.Text = "Double Click";
            if (!device.On)
            {
                return;
            }
            if (device.Error)
            {
                return;
            }
            //turn into setting mode, if it already is, turn to next setting area
            string settingStatus = "Null";
            switch (device.SetArea)
            {
                case Device.Setting.nothing:
                    device.SetArea = Device.Setting.unit;
                    settingStatus = "Setting Unit";
                    break;
                case Device.Setting.unit:
                    device.SetArea = Device.Setting.month;
                    settingStatus = "Setting Month";
                    break;
                case Device.Setting.month:
                    device.SetArea = Device.Setting.day;
                    settingStatus = "Setting Day";
                    break;
                case Device.Setting.day:
                    device.SetArea = Device.Setting.year;
                    settingStatus = "Setting Year";
                    break;
                case Device.Setting.year:
                    device.SetArea = Device.Setting.hour;
                    settingStatus = "Setting Hour";
                    break;
                case Device.Setting.hour:
                    device.SetArea = Device.Setting.minute;
                    settingStatus = "Setting Minute";
                    break;
                case Device.Setting.minute:
                    device.SetArea = Device.Setting.unit;
                    settingStatus = "Setting Unit";
                    break;
                default:
                    break;
            }
            //update the textBlock_Setting_Status
            textBlock_Setting_Status.Text = settingStatus;
            // flash the setting area
            timerFlash.Enabled = true;
            timerFlash.Start();
        }

        private void button_button_long_Click(object sender, RoutedEventArgs e)
        {
            action_button_longPress();
        }

        private void button_button_double_Click(object sender, RoutedEventArgs e)
        {
            action_button_doublePress();
        }

        private void timerFlash_Elapsed(object source, ElapsedEventArgs e)
        {
            if (DateTime.Now.Second%10 == 1)
            {
                textBlock_Button_Status.Text = "Hidden";
                image_resultUnit.Visibility = Visibility.Hidden;
            }
            else
            {
                textBlock_Button_Status.Text = "Visible";
                image_resultUnit.Visibility = Visibility.Visible;
            }
            switch (device.SetArea)
            {
                case Device.Setting.nothing:
                    break;
                case Device.Setting.unit:
                    //if (image_resultUnit.Visibility.Equals(Visibility.Visible))
                    //{
                    //    textBlock_AC_Status.Text = "Hidden";
                    //    image_resultUnit.Visibility = Visibility.Hidden;
                    //}
                    //else
                    //{
                    //    textBlock_AC_Status.Text = "Visible";
                    //    image_resultUnit.Visibility = Visibility.Visible;
                    //}
                    break;
                case Device.Setting.month:

                    break;
                case Device.Setting.day:
                    break;
                case Device.Setting.year:
                    break;
                case Device.Setting.hour:
                    break;
                case Device.Setting.minute:
                    break;
                default:
                    break;
            }

        }

        private void timerProgressiveBar_Elapsed(object source, ElapsedEventArgs e)
        {
            moveProgressiveBar();
        }

        private void button_progressiveBar_move_Click(object sender, RoutedEventArgs e)
        {
            moveProgressiveBar();
        }

        private void button_main_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            action_button_doublePress();
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void stackPanel_description_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //this.DragMove();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (Environment.OSVersion.Version.Major < 6)
            {
                MessageBox.Show("You may use Windows XP or earlier version, the effect will be reduced.");
                this.Background = Brushes.White;
                return;
            }
            try
            {
                AeroHelper.ExtendGlassFrame(this, new Thickness(-1));
                //AeroGlassHelper.ExtendGlass(this, 10, 10, 10, 10);
            }
            catch
            {
                // A DllNotFoundException occurs if you run this on Windows XP.
                // A Not Supported Exception is thrown if the DwmExtendFrameIntoClientArea() call fails.
                this.Background = Brushes.White;
            }
        }
    }
}
